package com.hongxun.modules.api.rest;

import com.alibaba.fastjson.JSON;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;
import com.hongxun.modules.bss.entity.DPictureEntity;
import com.hongxun.modules.bss.service.DPictureService;
import com.hongxun.modules.csg.entity.CArrivalRecordEntity;
import com.hongxun.modules.csg.entity.CGoodsDetailEntity;
import com.hongxun.modules.csg.entity.CStackPointEntity;
import com.hongxun.modules.csg.service.CArrivalRecordService;
import com.hongxun.modules.csg.service.CGoodsDetailService;
import com.hongxun.modules.csg.service.CStackPointService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.util.TextUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


/**
 * 材料到场
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-11 09:30:52
 */
@RestController
@RequestMapping("/rest/cstackpoint")
public class CStackPointRestController {
	@Autowired
	CStackPointService cStackPointService;
	@Autowired
	CArrivalRecordService cArrivalRecordService;
	@Autowired
	CGoodsDetailService cGoodsDetailService;

	
	/**
	 * 获取信息
	 */
	@RequestMapping("/getInfo")
	public R getInfo(@RequestParam Map<String, Object> params){
		//查询列表数据
		String pGuid = params.get("pGuid").toString();


        //1.堆放点
		Map<String ,Object> CStackPointMap = new HashMap<>();
		String whereCStackPoint = " 1=1 and p_guid='"+pGuid+"'";
		CStackPointMap.put("filterSql",whereCStackPoint+" ");
		List<CStackPointEntity> cStackPointEntityList = cStackPointService.queryList(CStackPointMap);


		//2.到货记录【一对多】。
		List<CArrivalRecordEntity> cArrivalRecordEntityList = new ArrayList<>();
		if (cStackPointEntityList != null) {
			for (CStackPointEntity cStackPointEntity : cStackPointEntityList) {
				Map<String , Object> cArrivalRecordMap = new HashMap<>();
				String cArrivalRecord = " 1=1 and s_p_guid='"+cStackPointEntity.getGuid()+"'";
				cArrivalRecordMap.put("filterSql",cArrivalRecord+" ");
				cArrivalRecordEntityList.addAll( cArrivalRecordService.queryList(cArrivalRecordMap));
			}
		}


		//3.物资清单【一对多】。
		List<CGoodsDetailEntity> cGoodsDetailEntityList = new ArrayList<>();
		if (cArrivalRecordEntityList != null) {
			for (CArrivalRecordEntity cArrivalRecordEntity : cArrivalRecordEntityList) {
				Map<String , Object> CGoodsDetailMap = new HashMap<>();
				String whereCGoodsDetail = "1=1 and a_r_guid='"+cArrivalRecordEntity.getGuid()+"'";
				CGoodsDetailMap.put("filterSql",whereCGoodsDetail+" ");
				cGoodsDetailEntityList.addAll(cGoodsDetailService.queryList(CGoodsDetailMap));
			}
		}


		return R.ok().put("cStackPointEntityList", JSON.toJSONString(cStackPointEntityList))
					 .put("cArrivalRecordEntityList",JSON.toJSONString(cArrivalRecordEntityList))
					 .put("cGoodsDetailEntityList",JSON.toJSONString(cGoodsDetailEntityList));
	}



	/**
	 * 同步上传
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping("/addCStackPoint")
	public R addCStackPoint(@RequestParam Map<String , Object> params){
		//1.新增堆放点
		String CStackPoint = params.get("cStackPointEntityList").toString();
		List<CStackPointEntity> cStackPointEntityList = JSON.parseArray(CStackPoint,CStackPointEntity.class);
		if (cStackPointEntityList != null) {
			cStackPointService.insertOrReplaceOrDelete(cStackPointEntityList);
		}


		//2.新增到货记录【一对多】  s_p_guid = 堆放点的 guid
		String CArrivalRecord = params.get("cArrivalRecordEntityList").toString();
		List<CArrivalRecordEntity> cArrivalRecordEntityList = JSON.parseArray(CArrivalRecord,CArrivalRecordEntity.class);
		if (cArrivalRecordEntityList != null) {
			cArrivalRecordService.insertOrReplaceOrDelete(cArrivalRecordEntityList);
		}

		//3.新增物资【一对多】 a_r_guid = 到货记录的 guid
		String CGoodsDetail = params.get("cGoodsDetailEntityList").toString();
		List<CGoodsDetailEntity> cGoodsDetailEntityList = JSON.parseArray(CGoodsDetail,CGoodsDetailEntity.class);
		if (cGoodsDetailEntityList != null) {
			cGoodsDetailService.insertOrReplaceOrDelete(cGoodsDetailEntityList);
		}


		return R.ok();
	}


}
